package hng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import hng.mapper.ProductCommentMapper;
import hng.mapper.ProductImgMapper;
import hng.mapper.ProductSpecMapper;
import hng.model.ProductDO;
import hng.mapper.ProductMapper;
import hng.model.ProductSpecDO;
import hng.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hng.vo.ProductCommentVO;
import hng.vo.ProductDetailVO;
import hng.vo.ProductSpecVO;
import hng.vo.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 告白
 * @since 2024-06-27
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> implements ProductService {

    @Autowired(required = false)
    private ProductMapper productMapper;

    @Autowired(required = false)
    private ProductSpecMapper productSpecMapper;

    @Autowired(required = false)
    private ProductCommentMapper productCommentMapper;

    @Autowired(required = false)
    private ProductImgMapper productImgMapper;

    /**
     * 模糊搜索商品
     */
    @Override
    public List<ProductVO> search(String keyword) {
        List<ProductVO> productList = productMapper.search(keyword);
        return productList;
    }

    /**
     * 通过分类id查找商品列表
     */
    @Override
    public List<ProductVO> searchCategory(Integer categoryId) {
        List<ProductDO> productDOList = productMapper.selectList(new LambdaQueryWrapper<ProductDO>()
                .eq(ProductDO::getCategoryId, categoryId)
                .orderByDesc(ProductDO::getBuyNum)
        );

        List<ProductVO> productVOList = productDOList.stream().map(obj -> {
            ProductVO productVO = new ProductVO();
            BeanUtils.copyProperties(obj, productVO);
            return productVO;
        }).collect(Collectors.toList());
        return productVOList;
    }

    /**
     * 查看商品详情
     */
    @Override
    public ProductDetailVO detail(Integer productId) {
        ProductDO productDO = productMapper.selectById(productId);
        List<ProductSpecDO> productSpecDOList = productSpecMapper.selectList(new LambdaQueryWrapper<ProductSpecDO>()
                .eq(ProductSpecDO::getProductId, productDO.getId())
        );
        List<ProductSpecVO> productSpecVOList = productSpecDOList.stream().map(obj -> {
            ProductSpecVO productSpecVO = new ProductSpecVO();
            BeanUtils.copyProperties(obj, productSpecVO);
            return productSpecVO;
        }).collect(Collectors.toList());

        // 查询图片列表
        List<String> imgList = productImgMapper.selectImgUrlsByProductId(productId);

        ProductDetailVO productDetailVO = new ProductDetailVO();
        BeanUtils.copyProperties(productDO, productDetailVO);
        productDetailVO.setSpecList(productSpecVOList);
        productDetailVO.setImgList(imgList);

        return productDetailVO;
    }

    /**
     * 查询商品评论列表
     */
    @Override
    public List<ProductCommentVO> commentList(Integer productId) {
        List<ProductCommentVO> commentList = productCommentMapper.getListByProductId(productId);
        return commentList;
    }

    /**
     * 查询销量前buyNum的商品详情
     */
    @Override
    public List<ProductVO> getTopSellingProducts(Integer buyNum) {
        List<ProductVO> topProducts = productMapper.getTopSellingProducts(buyNum);
        return topProducts;
    }

    /**
     * 查询销量前idNum的商品详情
     */
    @Override
    public List<ProductVO> getTopProducts(Integer idNum) {
        List<ProductVO> topIdProducts = productMapper.getTopProducts(idNum);
        return topIdProducts;
    }

}
